package cool.taomu.toolkit.jsch.module

import com.google.inject.AbstractModule
import com.google.inject.name.Names
import cool.taomu.toolkit.TaomuConfigure
import cool.taomu.toolkit.jsch.service.IJschConnection
import cool.taomu.toolkit.jsch.service.JschConnection
import org.apache.commons.beanutils.BeanUtils
import org.slf4j.LoggerFactory
import java.util.Map

class JschModule extends AbstractModule {
    val static LOG = LoggerFactory.getLogger(JschModule)
    TaomuConfigure config

    new(TaomuConfigure config) {
        this.config = config
    }

    override configure() {
        config.binder("taomu.jsch", [ it |
            if (it instanceof Map) {
                var connect = new JschConnection
                BeanUtils.populate(connect, it);
                var key = #["taomu.jsch.name", it.get("name")].join(".")
                LOG.info("jsch绑定key: {}", key);
                bind(IJschConnection).annotatedWith(
                    Names.named(key)
                ).toInstance(connect)
            }
        ])
    }
}
